package com.next.androidintentlibrary;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.app.Context;
import ohos.utils.net.Uri;

import java.io.File;

public class MediaIntents {
    private Context context;
    private Intent intent;
    private static final String AUDIO_TYPE = "audio/*";
    private static final String VIDEO_TYPE = "video/*";
    private static final String IMAGE_TYPE = "image/*";

    private MediaIntents(Context context)
    {
        this.context = context;
    }

    public static MediaIntents from(Context context)
    {
        return new MediaIntents(context);
    }

    // TODO:
    private MediaIntents playAudioFile(String path)
    {
        return playMediaFile(path, AUDIO_TYPE);
    }

    // TODO:
    private MediaIntents playAudioFile(File file)
    {
        return playMediaFile(file, AUDIO_TYPE);
    }

    public MediaIntents playAudio(String url)
    {
        return playMedia(url, AUDIO_TYPE);
    }

    // TODO:
    private MediaIntents showImageFile(String path)
    {
        return playMediaFile(path, IMAGE_TYPE);
    }

    // TODO:
    private MediaIntents showImageFile(File file)
    {
        return playMediaFile(file, IMAGE_TYPE);
    }

    public MediaIntents showImage(String url)
    {
        return playMedia(url, IMAGE_TYPE);
    }

    // TODO:
    private MediaIntents playVideoFile(File file)
    {
        return playMediaFile(file, VIDEO_TYPE);
    }

    // TODO:
    private MediaIntents playVideoFile(String path)
    {
        return playMediaFile(path, VIDEO_TYPE);
    }

    public MediaIntents playVideo(String url)
    {
        return playMedia(url, VIDEO_TYPE);
    }

    public MediaIntents playYouTubeVideo(String videoId)
    {
        try
        {
            intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            intent.setUri(Uri.parse("vnd.youtube:" + videoId));
            return this;
        } catch (Exception ex)
        {
            intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            intent.setUri(Uri.parse("http://www.youtube.com/watch?v=" + videoId));
            return this;
        }
    }

    private MediaIntents playMedia(String url, String type)
    {
        return playMedia(Uri.parse(url), type);
    }

    private MediaIntents playMediaFile(File file, String type)
    {
        return playMedia(Uri.getUriFromFile(file), type);
    }

    private MediaIntents playMediaFile(String path, String type)
    {
        return playMedia(Uri.getUriFromFile(new File(path)), type);
    }

    private MediaIntents playMedia(Uri uri, String type)
    {
        intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        intent.setUri(uri);
        intent.setType(type);
        return this;
    }

    public Intent build()
    {
        return intent;
    }

    private void startAbility(Intent intent)
    {
        if (!(context instanceof Ability))
        {
            intent.addFlags(Intent.FLAG_ABILITY_NEW_MISSION);
        }
        context.startAbility(intent,0);
    }

    public void show()
    {
        startAbility(build());
    }
}
